<?
include("ckeditor_config.php");	
switch($act){
	case "edit":	
	Edit();
	$title_page = "CMS - Chỉnh sửa hình ảnh trong sản phẩm";
	$tpl="edit";
	break;
	
	case "add":	
	$title_page = "CMS - Thêm sản phẩm";
	$tpl="edit";
	break;
	
	case "copy":
	ShowCopy();
	$tpl="copy";
	break;
	
	case "copysm":
	CopySm();
	break;
	
	case "del":
	Del();
	break;

	case "un_home":
	un_home();
	break;
	
	case "change_is_main_group":
	change_is_main_group();
	break;
	
	case "change_pro":
	change_pro();
	break;
	
	case "dellist":
	DelList();
	break;
	
	case "show":
	ChangeShow();
	break;
	
	case "hide":
	ChangeHide();
	break;
		
	case "order":
	Order();
	break;
	
	case "addsm":
	case "editsm":
	Editsm();
	break;
	
	case "change_special":
	change_special();
	break;
	
	case "change_home":
	change_home();
	break;
	
	case "change_active":
	change_active();
	break;
	
	default:
	ShowList();
	$title_page = "CMS - Sản phẩm";
	$tpl="list";
}

function ShowList()
{
	global $db,$stips,$page,$plpage,$set_per_page,$c;
	$set_per_page=20; 
	if(isset($_GET['product_id']) && $_GET['product_id']!=1 && $_GET['product_id']!='')
		$sql="select p.* from image_post_product p where p.product_id=".$_GET['product_id']." order by p.num asc, p.id desc ";
	else
		$sql="select * from image_post_product order by num asc, id desc ";
	$c = $db->numRows($db->query($sql));
	$plpage = plpage($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);	
	$stips = $db->getAll($sqlstmt);
	//echo $sqlstmt;
}

function Edit()
{
	global $db, $image_post_product, $product_id;
	$product_id=$_GET["product_id"];
	$sql = "select * from image_post_product where product_id=$product_id limit 6";
	$image_post_product = $db->getAll($sql);
	
	//print_r($image_post_product);
	//echo $sql;
}

function ShowCopy()
{
	global $db,$stips;
	$sql = "select c.id from categories c, products p where p.id=".$_GET['id']." and p.cid=c.id;";
	$r = $db->getRow($sql);
	$sql="select * from categories where  id<>".$r['id']." and comp=2 order by num asc, id asc";
	//id<>".$_GET['cid']." and
	$stips = array();
	DeQuiCp($stips, 345);
	//print_r($stips);
}

function DeQuiCp(&$Cats, $pid){
	global $db;
	$sql = "select id,pid,name_vn,has_child from categories where pid=$pid order by num asc, id asc";
	$r = $db->getAll($sql);
	if($r){
		foreach($r as $cat){
			if($cat['has_child'] == 1){
				DequiCp($Cats, $cat['id']);
			}
			else{
				$Cats[] = $cat;
			}
		}
	
	}
}

function CopySm()
{
	global $db;
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$arr = array();
		$arr['product_id'] = $_GET['id'];
		$arr['cat_id'] = $id[$i];
		vaInsert('asc_product_cat',$arr);
	}	
	$msg="Copy successfully";		
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function Order()
{
	global $db;
	$id=$_POST["id"];	
	$ordering=$_POST["ordering"];		
	for($i=0;$i<count($id);$i++){
		$sql="update products set num=".$ordering[$i]." where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Order successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function DelList()
{
	global $db;
	$id=$_POST["iddel"];		
	for($i=0;$i<count($id);$i++){
		$sqlstmt="select img from `products` where id=".$id[$i];
		$r = $db->getRow($sqlstmt);
		if(file_exists($r['img'])) unlink($r['img']);		
		
		$sql="delete from categories where pid=337 and alias=".$id[$i];
		$db->query($sql);	
		
		$sql="delete from products  where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Delete successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function change_special()
{
	global $db;
	$id=$_GET["id"];
	$cid = $_GET["cid"];
	
	$special = $_GET['current']==1?0:1;
	$sql="update products set special='$special' where id=$id";
	$db->query($sql);	
	
	$msg="Update successfully";	
		
	if(!empty($_GET['cid']))
		$page="admin.php?do=products&cid=".$_GET['cid'] . "&page=" . ($_GET['page']!=""?$_GET['page']:1);
	else
		$page="admin.php?do=products";
	$_SESSION['mess'] = $msg;
	page_transfer2($page);
	
}

function change_home()
{
	global $db;
	$id=$_GET["id"];
	$cid = $_GET["cid"];
	
	$home = $_GET['current']==1?0:1;
	$sql="update products set home='$home' where id=$id";
	$db->query($sql);	
	
	$msg="Update successfully";	
		
	if(!empty($_GET['cid']))
		$page="admin.php?do=products&cid=".$_GET['cid'] . "&page=" . ($_GET['page']!=""?$_GET['page']:1);
	else
		$page="admin.php?do=products";
	$_SESSION['mess'] = $msg;
	page_transfer2($page);
	
}

function change_active()
{
	global $db;
	$id=$_GET["id"];
	$cid = $_GET["cid"];
	
	$active = $_GET['current']==1?0:1;
	$sql="update products set active ='$active' where id=$id";
	$db->query($sql);	
	
	$msg="Update successfully";	
		
	if(!empty($_GET['cid']))
		$page="admin.php?do=products&cid=".$_GET['cid'] . "&page=" . ($_GET['page']!=""?$_GET['page']:1);
	else
		$page="admin.php?do=products";
	$_SESSION['mess'] = $msg;
	page_transfer2($page);
	
}
function ChangeShow()
{
	global $db;
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update products set active=1 where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function ChangeHide()
{
	global $db;
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update products set active=0 where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function Del()
{
	global $db;
	$id=$_GET["id"];

	$sqlstmt="select img from `products` where id=$id";
	$r = $db->getRow($sqlstmt);
	if(file_exists($r['img'])) unlink($r['img']);		
	$sql="delete from categories where pid=337 and alias=".$id;
	$db->query($sql);	
	$sql="delete from products  where id=".$id;
	$db->query($sql);	
		
	$msg="Delete successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function un_home()
{
	global $db;
	//$id=$_GET["id"];
//
//	$home = $_GET['current']==1?0:1;
//	$sql="update products set home=".$home." where id=".$id;
//	$db->query($sql);	
		
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update products set home=0 where id=".$id[$i];
		$db->query($sql);		
	}
	
	$msg="Update successfully";	
	$pagename = isset($_GET['pagename'])?$_GET['pagename']:'';
	
	if($pagename == 'item_home')
		$page="admin.php?do=item_home";
	else 
	{
		$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	}
	
	$_SESSION['mess'] = $msg;
	
	page_transfer2($page);
}
function change_home2()
{
	global $db;
	//$id=$_GET["id"];
//
//	$home = $_GET['current']==1?0:1;
//	$sql="update products set home=".$home." where id=".$id;
//	$db->query($sql);	
		
	$id=$_POST["iddel"];
	for($i=0;$i<count($id);$i++){
		$sql="update products set home=1 where id=".$id[$i];
		$db->query($sql);		
	}
	
	$msg="Update successfully";	
	$pagename = isset($_GET['pagename'])?$_GET['pagename']:'';
	
	if($pagename == 'item_home')
		$page="admin.php?do=item_home";
	else 
	{
		$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	}
	
	$_SESSION['mess'] = $msg;
	
	page_transfer2($page);
}
function change_is_main_group()
{
	global $db;
	$id=$_GET["id"];

	$home = $_GET['current']==1?0:1;
	$sql="update products set is_main_group=".$home." where id=".$id;
	$db->query($sql);	
		
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function change_pro()
{
	global $db;
	$id=$_GET["id"];

	$home = $_GET['current']==1?0:1;
	$sql="update products set special=".$home." where id=".$id;
	$db->query($sql);	
		
	$msg="Update successfully";		
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);
}
function Editsm()
{
	global $db,$act, $FullUrl;	
	
	$product_id = isset($_GET['product_id'])?$_GET['product_id']:'';
	for($i = 0; $i < 6; $i++)
	{
		if(isset($_FILES['img_'.($i)]['name'] ) && $_FILES['img_'.($i)]['size']>0){
			$file = $_FILES['img_'.($i)]['name'];
			$start = strpos($file,".");
			$type = substr($file,$start,strlen($file));
			//CheckUpload($type);
			$filename = substr($file,0, $start);
			$filename = strtolower($filename);
			
			$filename = $filename . '-' . time() . $type;
			
			copy($_FILES['img_'.($i)]['tmp_name'], "./upload/files/" . $filename) ;
			$arr[$i]['url'] = "upload/files/" . $filename;
			$arr[$i]['product_id'] = $product_id;
			$arr[$i]['active'] = '1';
		}
		
	}
	
	if ($act=="addsm")
	{
		$postId = vaInsert('image_post_product',$arr);
		$msg="Add successfully";	
		
	}
	else
	{
		$id_0 = isset($_GET['pr0'])?$_GET['pr0']:'0';
		$id_1 = isset($_GET['pr1'])?$_GET['pr1']:'0';
		$id_2 = isset($_GET['pr2'])?$_GET['pr2']:'0';
		$id_3 = isset($_GET['pr3'])?$_GET['pr3']:'0';
		$id_4 = isset($_GET['pr4'])?$_GET['pr4']:'0';
		$id_5 = isset($_GET['pr5'])?$_GET['pr5']:'0';
		
		for($i = 0; $i < 6; $i++)
		{
			if (isset($arr['img_'.$i])){
				$sqlstmt="select url from `image_post_product` where id=$id";
				$r = $db->getRow($sqlstmt);
				if(file_exists($r['url'])) unlink($r['url']);
			}
			
		}
		if($id_0 != '0')
			vaUpdate('image_post_product',$arr[0],' id='.$id_0);	
		else
			vaInsert('image_post_product',$arr[0]);	
		
		if($id_1 != '0')
			vaUpdate('image_post_product',$arr[1],' id='.$id_1);	
		else
			vaInsert('image_post_product',$arr[1]);	
			
		if($id_2 != '0')
			vaUpdate('image_post_product',$arr[2],' id='.$id_2);	
		else
			vaInsert('image_post_product',$arr[2]);	
			
		if($id_3 != '0')
			vaUpdate('image_post_product',$arr[3],' id='.$id_3);	
		else
			vaInsert('image_post_product',$arr[3]);			
		
		if($id_4 != '0')
			vaUpdate('image_post_product',$arr[4],' id='.$id_4);	
		else
			vaInsert('image_post_product',$arr[4]);	
			
		if($id_5 != '0')
			vaUpdate('image_post_product',$arr[5],' id='.$id_5);	
		else
			vaInsert('image_post_product',$arr[5]);	
				
		$msg="Đã Edit successfully";		
	}
	$_SESSION['mess'] = $msg;
	$page="admin.php?do=products". (isset($_GET['cid'])?'&cid='.$_GET['cid']:'') . (isset($_GET['page'])?'&page='.$_GET['page']:'');
	//print_r($arr);
	page_transfer2($page);
}

?>